package tosp.search;

import java.util.Vector;

import org.apache.log4j.Logger;

import tosp.HistoryMachine;
import tosp.JobSequence;
import tosp.job.Job;

public class TwoSwitchSearch extends LocalSearchAdapter {

	static Logger log = Logger.getLogger(TwoSwitchSearch.class);
	
	public TwoSwitchSearch(HistoryMachine m) {
		super(m);
	}

	/**
	 * Generiert Nachbarn, wobei bei jedem Nachbarn genau zwei Jobs vertauscht werden
	 */
	@Override
	public JobSequence[] getNeighbors(JobSequence js) {
		Vector<JobSequence> twoset = new Vector<JobSequence>();
		for (int index = 0; index < js.getNumberOfJobs()-2; index++) {
			Vector<Job> work = new Vector<Job>();
			for (int i=0; i<index; i++) {
				work.add(js.getJob(i));
			}
			work.add(js.getJob(index+1));
			work.add(js.getJob(index));
			for (int i=index+2; i<js.getNumberOfJobs(); i++) {
				work.add(js.getJob(i));
			}
			JobSequence newjs = new JobSequence(work,true);
			twoset.add(newjs);
			log.trace("Generated new Neighbor: "+newjs.getSeqAsString());
		}
		twoset.trimToSize();
		log.debug("Generated a Total of "+ twoset.size() +" Neighbors");
		return twoset.toArray(new JobSequence[twoset.size()]);
		// wird nicht genutzt (alle Vertauschen)
		/*for (int index = 0; index < jobs.length; index++) {
			for (int i = 0; i < jobs.length; i++) {
				if (i == index)
					continue;
				Vector<Job> work = new Vector<Job>();
				for (int j = 0; j < jobs.length; j++) {
					if (j == i) {
						simpleJob jo = new simpleJob(jobs[index]);
						work.add(jo);
					} else if (j == index) {
						simpleJob jo = new simpleJob(jobs[i]);
						work.add(jo);
					} else
						work.add(jobs[j]);
				}
				twoset.add(new JobSequence(work,true));
			}
		}*/
	}
	
}
